A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. The structure and properties of a socket are defined by an application programming interface (API) for the networking architecture. Sockets are created only during the lifetime of a process of an application running in the node.
Because of the standardization of the TCP/IP protocols in the development of the Internet, the term network socket is most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as Internet socket. In this context, a socket is externally identified to other hosts by its socket address, which is the triad of transport protocol, IP address, and port number.
The term socket is also used for the software endpoint of node-internal inter-process communication (IPC), which often uses the same API as a network socket.
The application programming interface (API) for the network protocol stack creates a handle for each socket created by an application, commonly referred to as a socket descriptor. In Unix-like operating systems, this descriptor is a type of file descriptor. It is stored by the application process for use with every read and write operation on the communication channel.
At the time of creation with the API, a network socket is bound to the combination of a type of network protocol to be used for transmissions, a network address of the host, and a port number. Ports are numbered resources that represent another type of software structure of the node. They are used as service types, and, once created by a process, serve as an externally (from the network) addressable location component, so that other hosts may establish connections.
Network sockets may be dedicated for persistent connections for communication between two nodes, or they may participate in connectionless and multicast communications.
In practice, due to the proliferation of the TCP/IP protocols in use on the Internet, the term network socket usually refers to use with the Internet Protocol (IP). It is therefore often also called Internet socket.
The application programming interface (API) that programs use to communicate with the protocol stack, using network sockets, is called a socket API. Development of application programs that utilize this API is called socket programming or network programming. Internet socket APIs are usually based on the Berkeley sockets standard. In the Berkeley sockets standard, sockets are a form of file descriptor, due to the Unix philosophy that "everything is a file", and the analogies between sockets and files. Both have functions to read, write, open, and close. In practice, the differences strain the analogy, and different interfaces (send and receive) are used on a socket. In inter-process communication, each end generally has its own socket.
In the standard Internet protocols TCP and UDP, a socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Sockets need not have a source address, for example, for only sending data, but if a program binds a socket to a source address, the socket can be used to receive data sent to that address. Based on this address, Internet sockets deliver incoming to the appropriate application process.
Socket often refers specifically to an internet socket or TCP socket. An internet socket is minimally characterized by the following:
Within the operating system and the application that created a socket, a socket is referred to by a unique integer value called a socket descriptor.
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
public class Main {
public static void main(String[] args) {
InetAddress address = InetAddress.getByName("203.0.113.0");
// IP = 203.0.113.0, port = 80
// the Socket is automatically closed at the end of the try block
// java.net.Socket uses TCP, while java.net.DatagramSocket uses UDP
try (Socket socket = new Socket(address, 80)) {
// writes to the output stream of the socket, with automatic flushing enabled
PrintWriter socketOut = new PrintWriter(socket.getOutputStream(), true);
socketOut.println("Hello, world!");
} catch (SocketException e) {
System.out.printf("An error occurred while accessing the socket: %s%n", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.printf("An error occurred while writing to socket: %s%n", e.getMessage());
e.printStackTrace();
}
}
}
The traditional Berkeley sockets usage in C would look like this:
int main() {
char message[] = "Hello, World!";
// Create socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
fprintf(stderr, "Failed to create socket!\n");
return 1;
}
// Set server address
struct sockaddr_in server_addr = {
.sin_family = AF_INET, // Address family
.sin_port = htons(80), // Port number (converted to network byte order)
.sin_addr.s_addr = inet_addr("203.0.113.0"), // IP address
};
// Connect to server
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
fprintf(stderr, "Connection failed!\n");
return 1;
}
// Send message
send(sockfd, message, strlen(message), 0);
printf("Message sent!\n");
// Close socket
close(sockfd);
return 0;
}
Other socket types are implemented over other transport protocols, such as Systems Network Architecture and Unix domain sockets for internal inter-process communication.
A TCP server may serve several clients concurrently by creating a unique dedicated socket for each client connection in a new child process or processing thread for each client. These are in the established state when a socket-to-socket virtual connection or virtual circuit (VC), also known as a TCP session, is established with the remote socket, providing a duplex byte stream.
A server may create several concurrently established TCP sockets with the same local port number and local IP address, each mapped to its own server-child process, serving its own client process. They are treated as different sockets by the operating system since the remote socket address (the client IP address or port number) is different; i.e., since they have different socket pair tuples.
UDP sockets do not have an established state, because the protocol is connectionless. A UDP server process handles incoming datagrams from all remote clients sequentially through the same socket. UDP sockets are not identified by the remote address, but only by the local address, although each message has an associated remote address that can be retrieved from each datagram with the networking application programming interface (API).
In c. 1987, AT&T introduced the STREAMS-based Transport Layer Interface (TLI) in UNIX System V Release 3 (SVR3). and continued into Release 4 (SVR4).
Other early implementations were written for TOPS-20, MVS, VM, IBM-DOS (PCIP). The Desktop Computer as a Network Participant.pdf 1985
are typically available in network equipment and are used for such as IGRP and OSPF, and for Internet Control Message Protocol (ICMP).
|
|